package com.example.asus.practice;

import android.hardware.Camera;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.AppCompatButton;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;

import java.io.File;
import java.io.IOException;

public class VEDIO extends AppCompatActivity {

    private AppCompatButton start,stop,play;
    private SurfaceView surfaceView;
    private File videoFile;
    private MediaRecorder mediaRecorder;
    private android.hardware.Camera camera;
    private boolean isrecord = false;//记录录像状态

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vedio);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        if(!android.os.Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            Toast.makeText(this, "请安装SD卡", Toast.LENGTH_SHORT).show();
        }
        camera = Camera.open();//开启摄像头
        camera.autoFocus(null);//设置自动聚焦
        /**设置摄像头参数，并开始录制视频*/
        surfaceView = (SurfaceView)findViewById(R.id.surfaceView);
        surfaceView.getHolder().setFixedSize(1920,1080);//设置surfaceView分辨率

        start = (AppCompatButton)findViewById(R.id.bt_start);
        stop = (AppCompatButton)findViewById(R.id.bt_stop);
        play = (AppCompatButton)findViewById(R.id.bt_play);

        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                record();
            }
        });

        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //停止录制并提示视频保存位置
                if(isrecord){
                    mediaRecorder.stop();
                    mediaRecorder.release();
                    Toast.makeText(VEDIO.this, "录像保存在："+ videoFile, Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
    /**录制视频方法*/
    private void record(){
        File path = new File(Environment.getExternalStorageDirectory()+"/Myvideo/");//视频保存的文件夹
        if(!path.exists()){
            path.mkdir();
        }
        String fileName = "video.mp4";//视频文件名称
        videoFile = new File(path,fileName);//视频文件
        mediaRecorder = new MediaRecorder();//实例化MediaReroder对象
        camera.setDisplayOrientation(90);//调整摄像头角度
        camera.unlock();//解锁摄像头
        mediaRecorder.setCamera(camera);//使用摄像头
        mediaRecorder.reset();
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);//使用摄像头获取图像
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);//设置视频输出格式为MP4
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);//设置声音编码格式
        mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);//设置视频编码格式
        mediaRecorder.setVideoEncodingBitRate(1920*1080);//设置清晰度
        mediaRecorder.setVideoSize(1920,1080);//设置视频尺寸
        mediaRecorder.setVideoFrameRate(10);//每秒十帧
        mediaRecorder.setOutputFile(videoFile.getAbsolutePath());//视频输出路径
        mediaRecorder.setPreviewDisplay(surfaceView.getHolder().getSurface());//设置使用的SurfaceView
        mediaRecorder.setOrientationHint(90);//调整播放视频的角度
        try {
            mediaRecorder.prepare();//准备录像
        } catch (IOException e) {
            e.printStackTrace();
        }
        mediaRecorder.start();
        Toast.makeText(this, "开始录制", Toast.LENGTH_SHORT).show();
        isrecord = true;//正在录制状态

    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onPause() {
        super.onPause();
        camera.stopPreview();//停止预览
        camera.release();
    }
}
